<?php

class M_chat extends CI_Model {
	
	public function insert_chat($booking_id, $msg, $by, $video = false){
		$data['booking_id'] = $booking_id;
		$data['message'] = $msg;
		$data['chat_by'] = $by;
		$data['time_sent'] = NOW;
		$this->db->insert('chat', $data);
		return $this->db->affected_rows() > 0 ? $this->db->insert_id() : FALSE;
	}
	
	public function get_chats($booking_id){
		$q = $this->db->get_where('chat', array('booking_id' => $booking_id));
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	public function getLatestChat($booking_id, $chat_id_string){
		$q = "SELECT * FROM chat WHERE booking_id = '$booking_id'";
		if($chat_id_string != '')
			$q = $q."AND id NOT IN ($chat_id_string)";
		$r = $this->db->query($q);
		return $r->num_rows() >= 1 ? $r->result() : FALSE;
	}
	
}

?>